package tt.dz.egobus.servicemanager.business.task.job;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.service.BusinessCacheService;

import java.util.Map;

/**
 * @author Anson
 * @date 17-8-9
 * @name 获取业务缓存的服务, 在既定任务中存在一个业务流水号的数据
 * @since 1.0.6
 */
public abstract class BusinessCacheGrabDynamicTask extends AbstractQuartzDynamicTask {

    public static final String BUSINESS_CACHE_JOB_KEY = "BUSINESS_CACHE_JOB_KEY";

    /**
     * @param jobDatamap
     * @author Anson
     * @date 17-8-9
     * @name 得到当前抢单缓存数据
     * @since 1.0.6
     */
    protected BusinessCache getBusinessCache(Map<String,Object> jobDatamap) {
        String businessId = getBusinessId(jobDatamap);
        if (businessId == null) {
            return null;
        }
        return this.getBusinessCache(businessId);
    }

    /**
     * @param businessId
     * @author Anson
     * @date 17-8-9
     * @name 得到当前抢单缓存数据
     * @since 1.0.6
     */
    protected BusinessCache getBusinessCache(String businessId) {
        if (businessId == null) return null;
        BusinessCacheService businessCacheService = this.getBusinessCacheSerice();
        return businessCacheService.get(businessId);
    }

    /**
     * @param jobDatamap
     * @author Anson
     * @date 17-8-9
     * @name 获取当前业务流水号
     * @since 1.0.6
     */
    protected String getBusinessId(Map<String,Object> jobDatamap) {
        if (jobDatamap == null || jobDatamap.isEmpty()) {
            return null;
        }
        Object obj = jobDatamap.get(BUSINESS_CACHE_JOB_KEY);
        String businessId = ObjectUtils.defaultIfNull(obj, "").toString();
        if (StringUtils.isBlank(businessId)) {
            return null;
        }
        return businessId;
    }

    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取当前business抢单缓存管理服务
     * @since 1.0.6
     */
    protected BusinessCacheService getBusinessCacheSerice() {
        return this.getSpringContext().getBean("redisBusinessCacheServiceImpl", BusinessCacheService.class);
    }
}
